# !/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: bonc qixin

import json
import time
# from urllib3 import request
# from urllib3 import exceptions
import urllib3


class RestAccess:
    def __init__(self, url, headers, data, method):
        self.http = urllib3.PoolManager()
        self.url = url
        self.headers = headers
        self.data = data
        self.method = method

    def access(self):
        try:
            # create_url = self.url  # the create request url
            # create_headers = {'cookie': 'the cooke'}  # the request headers
            # body_data_str = '{"body":"bodytext"}'
            # body_data = bytes(body_data_str, 'utf8')
            # req = self.http.request(create_url, headers=create_headers, data=body_data, method='POST')
            # encoded_data = json.dumps(self.data).encode('utf-8')

            resp = self.http.request(method=self.method, url=self.url, headers=self.headers, fields=self.data)
            # resp = self.http.urlopen(req)
            # result = resp.read().decode()
            # result_json = json.loads(result)
            result_json = json.loads(resp.data.decode('utf-8'))
            return result_json
        except urllib3.exceptions.HTTPError as err:
            # err.message
            # error_body = err.file.read().decode()
            return json.loads(err.message)


def huzyTest():
    req = RestAccess(url="https://qixin.bonc.com.cn/qixin/logic/adaptor.do?json&IFCODE=ifUserinfo",
                     method='POST',
                     headers={'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
                              'User-Agent': 'Apache-HttpClient/android10',
                              'Host': 'qixin.bonc.com.cn',
                              'Accept-Encoding': 'gzip;sdch;deflate',
                              'Connection': 'keep-alive'
                              },
                     data={'supplierCode': '46000',
                           'systemVersion': '29',
                           'uuid': '861d94be-319e-4096-a3e7-6db01e1644c3',
                           'appVersionCode': '3.2.0',
                           'cpuInfo': 'AArch64 Processor rev 13 (aarch64)',
                           'deviceName': 'PBEM00',
                           'ATOK': 'ee697a605bd224eef36b29b0e2409a3a',
                           'systemName': 'Android',
                           'jailbreak': '0',
                           'deviceManufacturer': 'OPPO',
                           'networkType': 'WIFI',
                           'androidId': 'f2afd97538763504',
                           'deviceType': 'android',
                           'supplierName': 'CMCC',
                           'systemId': '861d94be-319e-4096-a3e7-6db01e1644c3',
                           'phoneType': '1',
                           'serialNumber': '',
                           'coordinate': '112.567324,37.795433',
                           'deviceCustomName': 'PBEM00',
                           'systemVersionCode': '10',
                           'appVersionName': '3.2.0',
                           'deviceSerial': 'null',
                           'appResolution': '1080*2264',
                           'phoneMac': '14:C6:97:29:19:61',
                           'phoneIp': '192.168.137.195',
                           'imei': 'f2afd97538763504',
                           'supplierInternationalCode': 'cn',
                           'deviceModel': 'PBEM00',
                           'wifiMac': 'f6:8c:50:10:4e:06',
                           'region': '中国山西省太原市小店区晋阳街',
                           'basestationLac': 'GsmCellLocationBean{MCCMNC=\'46000\', LAC=14005, CELLID=56446239, BSSS=-1026}'
                           }
                     )
    return req


def HomePage():
    req = RestAccess(url="https://qixin.bonc.com.cn/qixin/logic/adaptor.do?json&IFCODE=getClockUserInfoNew",
                     method='POST',
                     headers={'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
                              'User-Agent': 'Apache-HttpClient/android10',
                              'Host': 'qixin.bonc.com.cn',
                              'Accept-Encoding': 'gzip;sdch;deflate',
                              'Connection': 'keep-alive'
                              },
                     data={'supplierCode': '46000',
                           'type': '0',
                           'uuid': '861d94be-319e-4096-a3e7-6db01e1644c3',
                           'systemVersion': '29',
                           'appVersionCode': '3.2.0',
                           'cpuInfo': 'AArch64 Processor rev 13 (aarch64)',
                           'deviceName': 'PBEM00',
                           'ATOK': 'ee697a605bd224eef36b29b0e2409a3a',
                           'systemName': 'Android',
                           'jailbreak': '0',
                           'deviceManufacturer': 'OPPO',
                           'networkType': 'WIFI',
                           'supplierName': 'CMCC',
                           'phoneType': '1',
                           'serialNumber': '',
                           'coordinate': '112.567331,37.795445',
                           'deviceCustomName': 'PBEM00',
                           'systemVersionCode': '10',
                           'appVersionName': '3.2.0',
                           'appResolution': '1080*2264',
                           'phoneMac': '14:C6:97:29:19:61',
                           'phoneIp': '192.168.137.195',
                           'imei': 'f2afd97538763504',
                           'supplierInternationalCode': 'cn',
                           'wifiMac': 'f6:8c:50:10:4e:06',
                           'region': '中国山西省太原市小店区晋阳街',
                           'basestationLac': 'GsmCellLocationBean{MCCMNC=\'46000\', LAC=14005, CELLID=56446239, BSSS=-1026}'
                           }
                     )
    return req


if __name__ == '__main__':
    req = HomePage()
    resp = req.access()
    print(resp)
    print(resp['message'])
    print(resp['code'])
